const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest
function post(option) {
    return new Promise((resolve, reject) => {const request = new XMLHttpRequest()
        request.open("POST", option.url, true);
        request.setRequestHeader("Content-Type","application/json");


        if (option.headers){
            Object.keys(option.headers).forEach(key => {
                request.setRequestHeader(key, option.headers[key])
            })
        }

        request.onreadystatechange = () => {
            if (request.readyState === 4) {
                if (request.status === 200) {
                    resolve(JSON.parse(request.responseText))
                } else {
                    reject(request.responseText)
                }

            }

        }

        request.send(JSON.stringify(option.data))
    })
}

module.exports = {
    post: post,
}